﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;

namespace CADDemo2025
{
    public class Command0213
    {
        double dis = 1;
        [CommandMethod("Demo0213")]
        public void Demo0213()
        {
            if (!UmUserMethod.GetEntity(out Curve cur, "选择打断线")) return;
            if (!UmUserMethod.GetDouble(out dis, "输入间隔距离", dis)) return;
            List<Curve> curs = UmUserMethod.SelectAllEntities<Curve>();
            curs = curs.FindAll(x => x.Layer == cur.Layer);
            curs.Remove(cur);
            List<Curve> deleteCurs = new List<Curve>();
            List<Curve> addCurs = new List<Curve>();
            foreach (var item in curs)
            {
                List<Point3d> pos = item.IntersectWith(cur);
                if (pos.Count > 0)
                {
                    List<Point3d> disPos = new List<Point3d>();

                    foreach (var p in pos)
                    {
                        double d = item.GetDistAtPoint(p);
                        if (d - dis > 0 || item.Closed) disPos.Add(item.GetPointAtDist(d - dis));
                        if (d + dis < item.GetDistanceAtParameter(item.EndParam) || item.Closed) disPos.Add(item.GetPointAtDist(d + dis));
                    }
                    if (disPos.Count > 0)
                    {
                        List<Curve> splCurs = item.GetSplitCurves(disPos);
                        foreach (var splcur in splCurs)
                        {
                            if (splcur.IntersectWith(cur).Count == 0)
                            {
                                addCurs.Add(splcur);
                            }
                        }
                        deleteCurs.Add(item);
                    }
                }
            }
            deleteCurs.EraseEntities();
            addCurs.AddEntities();
        }
    }
}
